package vek.XML.Parsers;

    
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import vek.TerminalSettings;

/**
 * Parses the terminal settings from the settings.xml files and saves them in the static class: Terminal Settings.
 * @author Christian, Michal og Jakob (Hold 1, gruppe 2)
 */
public class XMLParserSettings  extends DefaultHandler{
    
    private String tempElementData;
    private String updateServer;
    private String pathToUpdatefile;
    private String pathToXMLFiles;
    private int updateInterval;
    private int terminalCoordinateX;
    private int terminalCoordinateY;
    private String terminalName;
    private double pixelsPerMeter;
    
    public void endElement(String uri, String localName, String qName) throws SAXException {
        
        if( localName.equalsIgnoreCase("updateserver"))
        {
            updateServer = tempElementData;
        }
        
        else if( localName.equalsIgnoreCase("pathtoupdatefile"))
        {
            pathToUpdatefile = tempElementData;
        }
        
        else if( localName.equalsIgnoreCase("pathtoxmlfiles"))
        {
            pathToXMLFiles = tempElementData;
        }
        
        else if( localName.equalsIgnoreCase("updateintervalinminutes"))
        {
            updateInterval = Integer.parseInt(tempElementData);
        }
        
        else if( localName.equalsIgnoreCase("coordinatex"))
        {
            terminalCoordinateX = Integer.parseInt(tempElementData);
        }
        
        else if( localName.equalsIgnoreCase("coordinatey"))
        {
            terminalCoordinateY = Integer.parseInt(tempElementData);
        }
        
        else if( localName.equalsIgnoreCase("terminalname"))
        {
            terminalName = tempElementData;
        }
        else if( localName.equalsIgnoreCase("pixelspermeter"))
        {
            pixelsPerMeter = Double.parseDouble(tempElementData);
        }
        
        
        
        
    }
    
    public void characters(char[] ch, int start, int length) throws SAXException {
        
       tempElementData = new String(ch, start, length);
        
    }
    
    public void endDocument()
    {
        saveSettings();
    }
    
    public void saveSettings()
    {
        TerminalSettings.setSettings(updateServer, pathToUpdatefile, pathToXMLFiles, updateInterval, terminalCoordinateX, terminalCoordinateY, terminalName, pixelsPerMeter); 
        System.out.println("Settings saved");
    }

}
